package com.cslg.questionnaires.controller;

import com.cslg.questionnaires.entity.Student;
import com.cslg.questionnaires.entity.Teacher;
import com.cslg.questionnaires.service.AddQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class StudentAddQuestionController {
    @Autowired
    private AddQuestionService addQuestionService;

    @GetMapping("/StudentAddQuestion")
    public ModelAndView addQuestionPage(HttpSession session){
        Student student = (Student) session.getAttribute("user");
        String userType = (String)session.getAttribute("userType");
        String username = student.getStu_name();
        String account = student.getStu_id();
        HashMap<String,Object> data = new HashMap<>();
        data.put("userType",userType);
        data.put("username",username);
        data.put("account",account);
        return new ModelAndView("AddQuestion",data);
    }

    @PostMapping("/StudentAddQuestion/saveAddQuestion")
    @ResponseBody
    public HashMap<String,Object> saveAddQuestion(@RequestBody HashMap<String, Object> request, HttpSession session){
        Student student = (Student) session.getAttribute("user");
        String userType = (String)session.getAttribute("userType");
        int studentId = student.getId();
        HashMap<String,Object> msg = addQuestionService.saveQuestion(request,studentId,userType);
        return msg;
    }

    @PostMapping("/StudentAddQuestion/publishAddQuestion")
    @ResponseBody
    public HashMap<String,Object> publishAddQuestion(@RequestBody HashMap<String, Object> request, HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("user");
        String userType = (String)session.getAttribute("userType");
        int teacherId = teacher.getId();
        HashMap<String,Object> msg = addQuestionService.publishQuestion(request,teacherId,userType);
        return msg;
    }
}
